package com.apache.attachment.mommon;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * 文件操作工具类
 * @author zszhang@dwusoft.com
 */
public class FileUtil {

	private static FileUtil instance;

	private FileUtil() {
	}

	public static FileUtil getInstance() {
		if (null == instance) {
			instance = new FileUtil();
		}
		return instance;
	}

	public String getFileLength(long length) {
		double f = (double) Math.round(((double) length / 1024D) * 100D) / 100D;
		return f + "k";
	}

	/**
	 * 取得文件扩展名
	 * @param name
	 * @return String
	 */
	public String getExtension(String name) {
		int i = name.lastIndexOf('.');
		if (i >= 0)
			return name.substring(i + 1).toLowerCase();
		else
			return "";
	}

	/**
	 * 判断是否目录
	 * @param name 文件的具体位置 如F:/test
	 * @return boolean
	 */
	public boolean isDir(String name) {
		return (new File(name)).isDirectory();
	}

	/**
	 * 去除空格
	 * @param str
	 * @return String
	 */
	public String trim(String str) {
		if (str == null)
			return null;
		str = str.trim();
		if (str.length() == 0)
			return null;
		else
			return str;
	}

	/**
	 * 根据文件全路径取到文件名
	 * @param path
	 * @return String
	 */
	public String getFileName(String path) {
		try {
			char separatorChar = '/';
			int index = path.lastIndexOf(separatorChar);
			if (index < 0) {
				separatorChar = '\\';
				index = path.lastIndexOf(separatorChar);
			}
			return path.substring(index + 1);
		} catch (Exception exception) {
			return "Unknown";
		}
	}

	private boolean isUnicodeFile(String path) throws Exception {
		FileInputStream fi = new FileInputStream(path);
		byte bom[] = new byte[20];
		int n = fi.read(bom);
		for (int i = 0; i < n; i++)
			System.out.println(Integer.toHexString(bom[i]));

		fi.close();
		if (bom[0] == 255 && bom[1] == 254) {
			return true;
		} else {
			System.err.println(new String(bom));
			return false;
		}
	}

	/**
	 * 删除目录以及子目录
	 * @param dir
	 */
	public void delete(File dir) {
		if (dir.isDirectory()) {
			File files[] = dir.listFiles();
			if (files != null) {
				for (int i = 0; i < files.length; i++)
					delete(files[i]);

			}
			dir.delete();
		} else {
			dir.delete();
		}
	}

	/**
	 * 文件拷贝
	 * @param file
	 * @param target
	 * @throws IOException
	 */
	public void copy(File file, File target) throws IOException {
		if (file.isDirectory())
			return;
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			fi = new FileInputStream(file);
			fo = new FileOutputStream(target);
			byte buff[] = new byte[1024];
			for (int n = 0; (n = fi.read(buff)) != -1;)
				fo.write(buff, 0, n);

		} finally {
			if (fo != null)
				fo.close();
			if (fi != null)
				fi.close();
		}
	}

	/**
	 * 把一个文件流file写成目标文件中
	 * @param file
	 * @param fullFileName 目标文件的位置全称
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void writeFile(File file, String fullFileName) throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(fullFileName);
		int i = 0;
		byte[] buf = new byte[1024];
		while ((i = fis.read(buf)) != -1) {
			fos.write(buf, 0, i);
		}
		fos.close();
		fis.close();
		fos.flush();
	}

	/**
	 * 把一个文件流comFile写成目标文件中
	 * @param file
	 * @param fullFileName 目标文件的位置全称
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void writeFile(CommonsMultipartFile comFile, String fullFileName) throws FileNotFoundException, IOException {
		File  f =  new File(fullFileName);
		comFile.transferTo(f);
	}

	/**
	 * <b>
	 * 通过全路径/文件名创建目录
	 * </b>
	 * @param fullPathFileName ①可以通过全路径形式创建 ②全路径+文件名形式创建目录：假如路径中相应目录不存在，则创建目录
	 */
	public void makDir(String fullPathFileName) {
		String newLinuxPathName = fullPathFileName.replaceAll("\\\\", "/");
		int i = newLinuxPathName.lastIndexOf("/");

		String realfileName = newLinuxPathName.substring(i + 1, newLinuxPathName.length());
		File file = null;
		if (realfileName.indexOf(".") == -1) {
			file = new File(newLinuxPathName);
		} else {
			String filepath = newLinuxPathName.substring(0, i + 1);
			file = new File(filepath);
		}
		if (!file.exists()) {
			file.mkdirs();
		}
	}
}